const app = getApp()
const request = require('../../../utils/https.js')
import util from '../../../utils/util.js';
var userId, fromUser, count = 0;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    count: 0,
    Index: 0,
    loading:false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    userId = wx.getStorageSync('userId');
    const that = this;
    that.HistoryHot();
  },
  HistoryHot() {
    const that = this;
    request._get(getApp().Api_url + 'goods/searchHistoryHot', {
      userId: userId
    }, function (res) {
      if (res.data.code == 200) {
        console.log('搜索历史==>', res.data.data)
        that.setData({
          historyData: res.data.data.history,
          hotData: res.data.data.hot
        })
      }
    }, function () {})
  },

  onChange(e) {
    const that = this;
    console.log('e.detail===>', e.detail)
    if (!e.detail) {
      console.log('空')
      that.setData({ //初始化page
        pageNum: 1,
        search: false,
        value: e.detail
      });

    } else {
      console.log('不空')
      that.setData({
        search: true,
        value: e.detail
      })
    }
  },
  onSearch(e) {
    const that = this;
    console.log('搜索==>', e.detail)
    that.setData({
      prodName: e.detail
    })

    if (!e.detail) {
      console.log('空')
      wx.showToast({
        title: '请输入内容',
        icon: 'none'
      });

    } else {
      that.setData({
        loading:true
      })
      request._get(getApp().Api_url + 'goods/search', {
        userId: userId,
        prodName: e.detail,
        sorts: 1,
        pageSize: 100,
        pageNum: 1
      }, function (res) {
        if (res.data.code == 200) {
          console.log('搜索列表==>', res.data.data)
          that.setData({
            loading:false,
            goodsList: res.data.data
          })
        }
      }, function () {})
    }



  },
  //搜索功能
  /*   onSearch(e) {
      const that = this;
      request._get(getApp().Api_url + 'fans/entrySearch', {
        nickName: e.detail,
        userId: userId,
        pageNum: 1,
        pageSize: 10
      }, function (res) {
        if (res.data.errMsg == 'OK') {
          console.log('商品列表==>', res.data.data)
          that.setData({
            orderList: res.data.data
          })
        }
      }, function () {})
    }, */

  onCancel() {
    const that = this;
    wx.showToast({
      title: '取消',
      icon: 'none'
    });
    that.setData({
      goodsList: '',
      prodName: ''
    })
    that.HistoryHot();
  },

  onClear() {
    wx.showToast({
      title: '清空',
      icon: 'none'
    });
  },
  onSort(e) {
    const that = this;
    let Index = e.currentTarget.dataset.index;
    var sorts
    that.setData({
      Index: Index
    })
    if (Index == 0) { //综合
      sorts = 1
    } else if (Index == 1) { //销量
      sorts = 12
    } else if (Index == 2) { //价格
      count++
      that.setData({
        count: count
      })
      console.log('count===>', count)
      if (count % 2 == 0) {
        console.log('从高往低')
        sorts = 21
      } else {
        console.log('从低往高')
        sorts = 22
      }

    } else if (Index == 3) { //新品
      sorts = 31
    }

    request._get(getApp().Api_url + 'goods/search', {
      userId: userId,
      prodName: that.data.prodName,
      sorts: sorts,
      pageSize: 100,
      pageNum: 1
    }, function (res) {
      if (res.data.code == 200) {
        console.log('搜索列表==>', res.data.data)
        that.setData({
          goodsList: res.data.data
        })
      }
    }, function () {})


  },
  /*
    清楚历史记录
  */
  clearData() {
    console.log('清楚历史记录')
    const that = this;
    request._get(getApp().Api_url + 'goods/delHistory', {
      userId: userId
    }, function (res) {
      if (res.data.code == 200) {
        console.log('清除历史记录==>', res.data.data)
        that.HistoryHot();
      }
    }, function () {})
  },
  /*
  历史记录搜索
  */
  getGoods(e) {
    const that = this;
    that.setData({
      loading:true
    })
    let prodName = e.currentTarget.dataset.prodname;
    that.setData({
      prodName: prodName,
      value: prodName
    })
    request._get(getApp().Api_url + 'goods/search', {
      userId: userId,
      prodName: prodName,
      sorts: 1,
      pageSize: 100,
      pageNum: 1
    }, function (res) {
      if (res.data.code == 200) {
        console.log('搜索列表==>', res.data.data)
        that.setData({
          goodsList: res.data.data,
          loading:false
        })
      }
    }, function () {})

  },
  //查看详情页
  goOrder(e) {
    const that = this;
    console.log('111')
    if (userId == '' || userId == null || userId == 'undefined') {
      wx.reLaunch({
        url: '/pages/login/login?path=/pages/list/list'
      })
      return;
    }
    let goodsId = e.currentTarget.dataset.goodsid;
    let goodsType = e.currentTarget.dataset.kinds;
    let fast = e.currentTarget.dataset.fast;

    wx.navigateTo({
      url: '/pages/gift_details/gift_details?goodsId=' + goodsId + '&show=' + 0 + '&userId=' + fromUser + '&fast=' + fast
    })
  },
  //详情页拉起购物车
  goShopping(e) {
    const that = this;
    if (userId == '' || userId == null || userId == 'undefined') {
      wx.reLaunch({
        url: '/pages/login/login?path=/pages/list/list'
      })
      return;
    }
    let goodsId = e.currentTarget.dataset.goodsid;
    let goodsType = e.currentTarget.dataset.kinds;
    let fast = e.currentTarget.dataset.fast;

    wx.navigateTo({
      url: '/pages/gift_details/gift_details?goodsId=' + goodsId + '&show=' + 1 + '&userId=' + fromUser + '&fast=' + fast
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})